// pages/SCM/store/warehouse/warehouse.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    warehouses:[],
    hideFlag:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.getCurUserHasRoles();
      this.setData({
        scrollHeight: app.getScrollHeight(0)
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    that.toast = that.selectComponent("#toast")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    that.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  toItem: function (e) {
    console.log('toDetail>>', e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../warehouseItem/warehouseItem?id=' + id,
    })
  },
  getList: function(){
    let that = this
    //lbbService.getStockGoodsList({}, function (res) {
    lbbService.getWarehouseList({}, function (res) {
      that.setData({
        warehouses: res.data
      })
    })
  },
  bindName:function(e){
      this.setData({name:e.detail.value});
  },
  bindSortNum: function (e) {
    this.setData({ sortNum: e.detail.value});
  },
  saveItem: function(){
    let that=this;
    let name=that.data.name;
    let sortNum=that.data.sortNum;
    if (!name) {
      that.toast.showToast('请输入仓库名称');
      return;
    }
    sortNum = parseInt(sortNum);
    let params={name:name};
    if (!isNaN(sortNum)){
        params.sortNum=sortNum;
    }
    lbbService.saveWarehouse(params, function (res) {
      that.setData({ hideFlag: true });
      wx.showToast({
        title: '增加成功',
      })
      that.getList();
    });
  },
  
  addItem: function(e){
    this.setData({hideFlag:false});
  },
  hideModal:function(){
    this.setData({hideFlag:true});
  },
  stopMaoPao:function(){
    return true;
  },

        goodsCheckList:function(){
                wx.navigateTo({
                  url: '../goodsCheckList/goodsCheckList',
                })
        },
        entryOutRunningWater:function(){
                wx.navigateTo({
                        url: '../entryOutLog/entryOutLog',
                })
        },
        goodsApplyList: function () {
                wx.navigateTo({
                        url: '../goodsApplyList/goodsApplyList',
                })
        },
        goodsSendList: function () {
                wx.navigateTo({
                  url: '../goodsSendList/goodsSendList',
                })
        },
        waitReceiveList: function () {
                wx.navigateTo({
                  url: '../waitReceiveList/waitReceiveList',
                })
        },
        goodsApplyStockUpList: function () {
          wx.navigateTo({
            url: '../goodsApplyStockUpList/goodsApplyStockUpList',
          })
        },
        ownerGoods: function () {
                let that=this;
                let uid='';
                let uname='';
                if(that.data.curUser){
                  uid = that.data.curUser.masterUid;
                  uname = that.data.curUser.masterName;
                }
                if(!uid){//uid合并后，使用新uid可能会undefined
                  uid=0;
                }
                let isStockAdmin = that.data.isStockAdmin?1:0
                let params = "?uid=" + uid + "&uname=" + uname + "&isStockAdmin=" + isStockAdmin;
                wx.navigateTo({
                  url: '../ownerGoods/ownerGoods' + params,
                })
        },
        goods: function () {
                wx.navigateTo({
                  url: '../goods/goods',
                })
        },

        getCurUserHasRoles:function(){
          let that=this;
          lbbService.getCurUserHasRoles({}, function (res) {
            let curUser={};
            let isStockAdmin=false;
            if(res.data){
              curUser=res.data;
              if(res.data.roleIds){
                let arr = res.data.roleIds.split(',');
                if (arr && arr.indexOf('12')!=-1){
                  isStockAdmin=true;
                }
              }
            }
            that.setData({ curUser: curUser, isStockAdmin: isStockAdmin});
          });
        },


  formSubmit: function (e) {
    console.log(e.detail.value);
    let name = e.detail.value.name;
    let sortNum = e.detail.value.sortNum;
    sortNum = sortNum ? parseInt(sortNum) : 0;
    if (!name) {
      this.toast.showToast('请输入仓库名称');
      return;
    }
    let that = this;
    lbbService.saveWarehouse({ name: name, sortNum: sortNum }, function (res) {
      wx.showToast({
        title: '增加成功',
      })
      that.getList();
    });
  },
  
})